PROJPATH			:= $(abspath ../../../../)
# also for Kconfig
export BOARDPATH	:= $(abspath ./)

export KCONFIG_TOOL	:= kconfig-mconf
export PYTHON_TOOL	:= python
OUTPUT				:= $(BOARDPATH)/build
# compiler arguments
FROMELF				:= fromelf
export TARGET		:= matrix
export CC			:= armclang
export AS			:= armclang
export LD			:= armlink
export CFLAGS		:= -xc -std=c99 --target=arm-arm-none-eabi -mcpu=cortex-m4 -mfpu=none -mfloat-abi=soft -c	\
					   -fno-rtti -funsigned-char -fshort-enums -fshort-wchar -gdwarf-4 -ffunction-sections -w	\
					   -DSTM32F407xx -DUSE_STDPERIPH_DRIVER -MD -O0 -g
export SFLAGS		:= --target=arm-arm-none-eabi -mcpu=cortex-m4 -mfpu=none -mfloat-abi=soft -masm=auto  -Wa,armasm,--diag_suppress=A1950W -c	\
					   -gdwarf-4 -Wa,armasm,--pd,"STM32F407xx SETA 1" -O0 -g
export LFLAGS		:= --cpu=Cortex-M4.fp.sp --strict --scatter "$(BOARDPATH)/meta/board/link.lds"	\
					   --datacompressor off --summary_stderr --info summarysizes --map --load_addr_map_info --xref --callgraph --symbols	\
					   --info sizes --info totals --info unused --info veneers --fpu=SoftVFP	\
					   --list "$(OUTPUT)/$(TARGET).map"
# source file path
export SRCDIRS		:=  $(BOARDPATH)/src	\
						$(BOARDPATH)/drv	\
						$(BOARDPATH)/../fwlib/CMSIS/STM32F4xx/Source	\
						$(BOARDPATH)/../fwlib/STM32F4xx_StdPeriph_Driver/src
# header file path
export INCDIRS		:=  $(BOARDPATH)		\
						$(BOARDPATH)/src	\
						$(BOARDPATH)/drv	\
						$(BOARDPATH)/../fwlib/CMSIS/Include	\
						$(BOARDPATH)/../fwlib/STM32F4xx_StdPeriph_Driver/inc	\
						$(BOARDPATH)/../fwlib/CMSIS/STM32F4xx/Include
# ignore files
export IGNORES		:=  $(BOARDPATH)/../fwlib/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cryp*.c	\
						$(BOARDPATH)/../fwlib/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_*wdg.c	\
						$(BOARDPATH)/../fwlib/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spi.c
# meta
export META_SRCDIRS	:= $(BOARDPATH)
export META_INCDIRS	:= $(BOARDPATH)
export META_CC		:= armclang --target=arm-arm-none-eabi -mcpu=cortex-m4 -E -P

# set default target
.DEFAULT_GOAL 		:= ALL

# -------- rules
ALL:
	@rm -rf $(OUTPUT)/$(TARGET).elf
	@$(MAKE) -C $(PROJPATH)
	@$(FROMELF) --i32 $(OUTPUT)/$(TARGET).elf --output $(OUTPUT)/$(TARGET).hex
	@$(FROMELF) -c $(OUTPUT)/$(TARGET).elf > $(OUTPUT)/$(TARGET).dump

%_defconfig:
	@$(MAKE) -C $(PROJPATH) $@

# -------- pseudo target
.PHONY: menuconfig clean distclean help download debug

menuconfig:
	@$(MAKE) -C $(PROJPATH) menuconfig
clean:
	@$(MAKE) -C $(PROJPATH) clean
distclean:
	@$(MAKE) -C $(PROJPATH) distclean
help:
	@$(MAKE) -C $(PROJPATH) help
# automatically download to RAM or FLASH
download:
	@ST-LINK_CLI.exe -c port=SWD -P $(OUTPUT)/$(TARGET).hex -v -rst
debug:
	@openocd.exe -f ./openocd/stlink-v2.cfg -f ./openocd/stm32f4x.cfg